package com.TeacherInfoServer.converter.handler;

import com.TeacherInfoServer.common.Result;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
import com.TeacherInfoServer.exception.*;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BindException.class)
    public Result<String> handleBindException(BindException e) {
        List<FieldError> allErrors = e.getFieldErrors();
        String errorMessage = allErrors.stream()
                .map(error -> error.getField() + ": " + error.getDefaultMessage())
                .collect(Collectors.joining(", "));
        return Result.fail(100, "参数不合法", errorMessage);
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public Result<String> handleConstraintViolationException(ConstraintViolationException ex) {
        Set<ConstraintViolation<?>> violations = ex.getConstraintViolations();
        String errorMessage = violations.stream()
                .map(violation -> violation.getPropertyPath() + ": " + violation.getMessage())
                .collect(Collectors.joining(", "));
        return Result.fail(100, "文件数据不合法", errorMessage);
    }

    @ExceptionHandler(LoginStatusInvalidException.class)
    public Result<?> handleLoginStatusInvalidException(){
        return Result.fail(101,"登录状态失效",null);
    }

    @ExceptionHandler(JobNumberNotExistException.class)
    public Result<?> handleJobNumberNotExistException(){
        return Result.fail(108,"工号不存在",null);
    }

    @ExceptionHandler(TeacherNameNotExistException.class)
    public Result<?> handleTeacherNameNotExistException(TeacherNameNotExistException e){
        return Result.fail(109,e.getMessage(),null);
    }
    @ExceptionHandler(DepartmentNameNotExistException.class)
    public Result<?> handleDepartmentNameNotExistException(){return Result.fail(110,"教研室不存在",null);}
    @ExceptionHandler(DepartmentNameNotExistDetailException.class)
    public Result<?> handleDepartmentNameNotExistDetailException(DepartmentNameNotExistDetailException e){
        return Result.fail(110,e.getMessage(),null);
    }

    @ExceptionHandler({SignatureVerificationException.class, AlgorithmMismatchException.class, JWTDecodeException.class})
    public Result<?> handleParseJwtException(){
        return Result.fail(113,"token无效",null);
    }

    @ExceptionHandler(RecordExistException.class)
    public Result<?> handleRecordExistException(){
        return Result.fail(201,"记录已存在",null);
    }
    @ExceptionHandler(RecordExistDetailException.class)
    public Result<?> handleRecordExistDetailException(RecordExistDetailException e){
        return Result.fail(201,e.getMessage(),null);
    }

    @ExceptionHandler(IOException.class)
    public Result<?> handleIOException(){
        return Result.fail(202,"操作失败",null);
    }

    @ExceptionHandler(JobNumberRepeatedException.class)
    public Result<?> handleJobNumberRepeatedException(){
        return Result.fail(203,"存在工号重复",null);
    }

    @ExceptionHandler(RecordRepeatedException.class)
    public Result<?> handleRecordRepeatedException(){
        return Result.fail(204,"存在记录重复",null);
    }
    @ExceptionHandler(TypeInfoNotExistException.class)
    public Result<?> handleTypeInfoNotExistException(TypeInfoNotExistException e){
        return Result.fail(205, e.getMessage(), null);
    }
    @ExceptionHandler(JobNumberRepeatedDetailException.class)
    public Result<?> handleJobNumberRepeatedDetailException(JobNumberRepeatedDetailException e){
        return Result.fail(203,e.getMessage(),null);
    }

}
